package DBLayer;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;


import ModelLayer.Gun;

public class DBGun {
	private static Connection con;
	Statement stmt;
	public DBGun() 
	{
		con = DbConnection.getInstance().getDBcon();
	}
	public void createGun(String fabric,String caliber)
	{
		try{
			int gID=GetMax.getMaxId("Select max(gID) from Gun")+1;//Gets the maximum gID from the DB and adds+1 for the next ID
			String query ="INSERT INTO Gun(gID, fabric, caliber)  VALUES('"+gID+"','"+fabric+"','"+caliber+"')";
			 Statement stmt = con.createStatement();
	          stmt.setQueryTimeout(5);
	     	  stmt.executeUpdate(query);
	          stmt.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			}
	}
	public void deleteGun(int gID)
	{	//gID=1; 
		try{
			String query="DELETE FROM Gun WHERE gID = '"+gID+"'";
			Statement stmt = con.createStatement();
	          stmt.setQueryTimeout(5);
	     	  stmt.executeUpdate(query);
	          stmt.close();
		}
		catch(Exception e)
		{e.printStackTrace();
		}
	}
	public void updateGun(Gun g)
	{ Gun gObj = g;
	String query="UPDATE gun SET "+
		 	  "fabric ='"+ gObj.getFabric()+"', "+
		 	  "caliber ='"+ gObj.getCaliber() + "'" +
		          " WHERE gID = '"+ gObj.getgID() + "'";
              System.out.println("Update query:" + query);
              try{
            	  Statement stmt = con.createStatement();
            	  stmt.setQueryTimeout(5);
            	  stmt.executeUpdate(query);
            	  stmt.close();
            	  
              }
              catch(Exception e)
              {e.printStackTrace();}
		
	}
	
	private String buildQuery()
	{
	    String query="SELECT gID,fabric,caliber  FROM Gun";
		/*
		if (wClause.length()>0)
			query=query+" WHERE ";
			*/
		return query;
	}
	public ArrayList<Gun> miscWhere()
	{
            ResultSet results;
	    ArrayList<Gun> list = new ArrayList<Gun>();	
		
	    String query =  buildQuery();
  
            try{ // read the employee from the database
		Statement stmt = con.createStatement();
	 	stmt.setQueryTimeout(5);
	 	results = stmt.executeQuery(query);
	 	
	
		while( results.next() ){
	     	 Gun empObj = new Gun();
		 empObj = buildGun(results);	
                 list.add(empObj);	
		}//end while
                 stmt.close();     
                 
			
		}//slut try	
	 	catch(Exception e){
	 		System.out.println("Query exception - select: "+e);
			e.printStackTrace();
	 	}
		return list;
	}
	
	public Gun findGun(int gID)
	{
		ResultSet results;
		Gun empObj = new Gun();
                
	        String query =  buildQuery() + " WHERE gID ="+"'"+gID+"'";
                System.out.println(query);
		try{ // 
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		
	 		if( results.next() ){
                            empObj = buildGun(results);
                            //assocaition is to be build
                            stmt.close();
                            
                            
			}
                        else{ //no employee was found
                            empObj = null;
                        }
		}//end try	
	 	catch(Exception e){
	 		System.out.println("Query exception: "+e);
	 	}
		return empObj;
	}
	private Gun buildGun(ResultSet results)
    {   Gun gObj = new Gun();
        try{ // the columns from the table glayee  are used
              gObj.setgID(results.getInt("gID"));
              gObj.setFabric(results.getString("fabric"));
              gObj.setCaliber(results.getString("caliber"));
              
        }
       catch(Exception e)
       {
           System.out.println("error in building the Gun object");
       }
       return gObj;
}
	
}